package com.iie.bigVideo.controller;


import com.iie.bigVideo.entity.Catalog;
import com.iie.bigVideo.entity.dto.CatalogDto;
import com.iie.bigVideo.service.CatalogService;
import com.iie.bigVideo.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/catalog")
@CrossOrigin //跨域
public class CatalogController {

    @Autowired
    private CatalogService catalogService;


    /**
     * 查询 目录 和 册
     *
     * @return
     */
    @GetMapping("/queryCatalogObject")
    public ResultVO<List<CatalogDto>> queryCatalogObject() {
        return catalogService.queryCatalog();
    }


    /**
     * 查询 课件
     */
    @GetMapping("/queryCatalogList/{current}/{size}")
    public ResultVO queryCatalogList(@PathVariable("current") int current, @PathVariable("size") int size) {

        return catalogService.queryCatalogList(current, size);
    }

    /**
     * 新增 目录 和 册
     * {
     * catalogName   //目录/册名
     * catalogLast //上一级: 新增 目录 不用传；新增 册 时传目录ID
     * catalogAssert  //生效时间范围：新增 目录 传；新增 册 时不传
     * imgFile  //封面图：新增 一级 不传；新增 二级 时传
     * }
     *
     * @return
     */

    @PostMapping("/addCatalog")
    public ResultVO addCatalogDirectory(@RequestParam("catalogName") String catalogName,
                                        @RequestParam(value = "catalogLast", required = false) String catalogLast,
                                        @RequestParam(value = "catalogAssert", required = false) String catalogAssert,
                                        @RequestParam(value = "imgFile", required = false) MultipartFile imgFile) {
        Catalog catalog = new Catalog();
        catalog.setCatalogName(catalogName);
        if (catalogLast != null && !catalogLast.isEmpty()) {
            catalog.setCatalogLast(Integer.valueOf(catalogLast));
        }
        catalog.setCatalogAssert(catalogAssert);
        return catalogService.addCatalogDirectory(catalog, imgFile);
    }

    /**
     * 修改 目录 和 册
     * {
     * catalogId   //目录/册 的id
     * catalogName  //目录/册名
     * catalogLast  //上一级： 修改目录不用传；修改册时传目录ID
     * catalogAssert  //生效时间范围: 修改 目录 传；修改册 时不传
     * }
     *
     * @return
     */
    @PostMapping("/updateCatalog")
    public ResultVO updateCatalog(@RequestParam("catalogId") String catalogId,
                                  @RequestParam("catalogName") String catalogName,
                                  @RequestParam(value = "catalogLast", required = false) String catalogLast,
                                  @RequestParam(value = "catalogAssert", required = false) String catalogAssert,
                                  @RequestParam(value = "imgFile", required = false) Object imgFile) {
        Catalog catalog = new Catalog();
        if (catalogId != null && !catalogId.isEmpty()) {
            catalog.setCatalogId(Integer.parseInt(catalogId));
        }
        catalog.setCatalogName(catalogName);
        if (catalogLast != null && !catalogLast.isEmpty()) {
            catalog.setCatalogLast(Integer.valueOf(catalogLast));
        }
        catalog.setCatalogAssert(catalogAssert);
        if (imgFile instanceof MultipartFile) {
            // 文件类型
            MultipartFile multipartFile = (MultipartFile) imgFile;
            return catalogService.updateCatalog(catalog, multipartFile);
        } else {
            // 其他类型
            return catalogService.updateCatalog(catalog, null);
        }

    }


    /**
     * 删除 目录/册
     *
     * @param catalogId 目录id 或者 册Id
     * @return
     */
    @GetMapping("/deleteCatalog")
    public ResultVO deleteCatalog(@RequestParam("catalogId") int catalogId) {
        return catalogService.deleteCatalog(catalogId);
    }

}

